#ifndef WSYMESH_H
#define WSYMESH_H

#include "geometry/geometry.h"
#include "wsymath/wsymath.h"

/** 包含了移动、旋转、缩放信息的几何体对象 */
class WSYMesh : public QObject {
public:
    WSYMesh() {
        Matrix3D::instance(this->matrix);
        WSYMesh::initializeDoubleArray(this->position, 0);
        WSYMesh::initializeDoubleArray(this->rotation, 0);
        WSYMesh::initializeDoubleArray(this->scale, 1);

        scaleMatrixNew = Matrix3D::newInstance();
        rotationMatrixNew = Matrix3D::newInstance();
        scaleRotationMatrixNew = Matrix3D::newInstance();
        transformMatrixNew = Matrix3D::newInstance();
        allMatrix = Matrix3D::newInstance();

        qDebug() << "the justice and the fairness";
        qDebug() << scale[1];
        qDebug() << "never forget";

        v0 = Math3D::newInstanceWithValue(0, 0, 0);
        vX = Math3D::newInstanceWithValue(1, 0, 0);
        vY = Math3D::newInstanceWithValue(0, 1, 0);
        vZ = Math3D::newInstanceWithValue(0, 0, 1);

        dirX = Math3D::newInstance();
        dirY = Math3D::newInstance();
        dirZ = Math3D::newInstance();
    }

    /** 重新设置三个方向 */
    virtual void resetThreeDirections() {
        Math3D::directionNew2(v0, vX, dirX);
        Math3D::directionNew2(v0, vY, dirY);
        Math3D::directionNew2(v0, vZ, dirZ);
    }

    /** 初始化位置 */
    static void initializeDoubleArray(double *array, double value) {
        array[0] = value; array[1] = value; array[2] = value;
    }

    /** 获取变换矩阵 */
    virtual GLfloat *getMatrix() {
        Matrix3D::scaleNew(this->scale[0], this->scale[1], this->scale[2], scaleMatrixNew);

        computeAngleOfEular();

        Matrix3D::moveNew(this->position[0], this->position[1], this->position[2], transformMatrixNew);

        //printSingleMatrix("transform", transformMatrix);

//        QList<GLfloat*> *matrixArray = new QList<GLfloat*>();
//        matrixArray->push_back(scaleMatrixNew);
//        matrixArray->push_back(rotationMatrixNew);
//        matrixArray->push_back(transformMatrixNew);

        Matrix3D::multiplyTwoMatrices(scaleMatrixNew, rotationMatrixNew);
        Matrix3D::multiplyTwoMatrices(rotationMatrixNew, transformMatrixNew);

        Matrix3D::appendValuesFromMatrix(transformMatrixNew, allMatrix);

        //matrixArray = NULL;
        //scaleMatrix = NULL; rotationMatrix = NULL; transformMatrix = NULL;

        return allMatrix;
    }
    /** 设置位置 */
    virtual void setPosition(double x, double y, double z) {
        position[0] = x; position[1] = y; position[2] = z;
    }
    /** 设置旋转 */
    virtual void setRotation(double x, double y, double z) {
        rotation[0] = x; rotation[1] = y; rotation[2] = z;
    }
    /** 设置旋转 */
    virtual void setRotationOfAngle(double x, double y, double z) {
        rotation[0] = WSYMath::DegreeToRadian(x);
        rotation[1] = WSYMath::DegreeToRadian(y);
        rotation[2] = WSYMath::DegreeToRadian(z);
    }
    /** 设置缩放 */
    virtual void setScale(double x, double y, double z) {
        scale[0] = x; scale[1] = y; scale[2] = z;
    }
    /** 设置旋转的欧拉角 */
    virtual void computeAngleOfEular();
    Geometry *object;
private:
    double position[3]; //位置信息
    double rotation[3]; //旋转参数
    double scale[3]; //尺寸
    double matrix[16]; //变换矩阵
    GLfloat *scaleMatrixNew; //缩放矩阵
    GLfloat *rotationMatrixNew; //旋转矩阵
    GLfloat *scaleRotationMatrixNew; //缩放旋转矩阵
    GLfloat *transformMatrixNew; //变换矩阵
    GLfloat *allMatrix; //所有的矩阵

    GLfloat *v0, *vX, *vY, *vZ;
    GLfloat *dirX, *dirY, *dirZ;
};

#endif // WSYMESH_H
